module.exports = app => {
  return async (ctx, next) => {
    ctx.socket.emit('res', 'connected!');

    const { app, socket, logger, helper } = ctx;
    const id = socket.id;
    const nsp = app.io.of('/');
    const query = socket.handshake.query;

    // 用户信息

    const { room, userId } = query;

    if (room != undefined) {
      const rooms = [room];
      logger.debug('#user_info', id, room, userId);
      ctx.session[userId] = id
      ctx.room("room", room)
      ctx.room(userId, id)
      console.log("当前用户名:%s,socketId :%s ", userId, id);

      // 用户加入
      logger.debug('#join', room);
      socket.join(room);
    }


    /*// 在线列表
    nsp.adapter.clients(rooms, (err, clients) => {
      logger.debug('#online_join', clients);

      // 更新在线用户列表
      nsp.to(room).emit('online', {
        clients,
        action: 'join',
        target: 'participator',
        message: `User(${id}) joined.`,
      });
    });
    */
    await next();
    // execute when disconnect.
    console.log('disconnection!');
  };
};